package com.example.logindemo.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SecurityUtil {
    
    /**
     * 获取当前登录用户的用户名
     */
    public static String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            return authentication.getName();
        }
        return null;
    }
    
    /**
     * 获取当前登录用户的所有权限
     */
    public static Collection<? extends GrantedAuthority> getCurrentUserAuthorities() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            return authentication.getAuthorities();
        }
        return List.of();
    }
    
    /**
     * 获取当前登录用户的所有权限名称
     */
    public static List<String> getCurrentUserAuthorityNames() {
        return getCurrentUserAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.toList());
    }
    
    /**
     * 检查当前用户是否有指定角色
     */
    public static boolean hasRole(String role) {
        return hasAuthority("ROLE_" + role);
    }
    
    /**
     * 检查当前用户是否有任意一个指定角色
     */
    public static boolean hasAnyRole(String... roles) {
        Collection<String> authorities = getCurrentUserAuthorityNames();
        for (String role : roles) {
            if (authorities.contains("ROLE_" + role)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 检查当前用户是否有指定权限
     */
    public static boolean hasAuthority(String authority) {
        return getCurrentUserAuthorityNames().contains(authority);
    }
    
    /**
     * 检查当前用户是否有任意一个指定权限
     */
    public static boolean hasAnyAuthority(String... authorities) {
        Collection<String> userAuthorities = getCurrentUserAuthorityNames();
        for (String authority : authorities) {
            if (userAuthorities.contains(authority)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 检查当前用户是否已认证
     */
    public static boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication != null && authentication.isAuthenticated() && 
               !"anonymousUser".equals(authentication.getName());
    }
    
    /**
     * 检查当前用户是否是管理员
     */
    public static boolean isAdmin() {
        return hasRole("ADMIN");
    }
    
    /**
     * 检查当前用户是否是普通用户
     */
    public static boolean isUser() {
        return hasRole("USER");
    }
    
    /**
     * 检查当前用户是否是版主/审核员
     */
    public static boolean isModerator() {
        return hasRole("MODERATOR");
    }
}